<?php

class Apk_Util
{
	static $locale = 'ru_RU.UTF-8';
	public static function getDayStart($timestamp)
    {
        return mktime(0,0,0,date('m', $timestamp), date('d',$timestamp), date('Y', $timestamp));
    }

    public static function getDayEnd($timestamp)
    {
        return mktime(23, 59, 59, date('m', $timestamp), date('d',$timestamp), date('Y', $timestamp));
    }

    /**
     * Method for convert timestamp to array, for used at decorators
     *
     * @param integer $time
     * @return array
     *
     */
    public static function timestampToArray($time, $locale='ru_RU')
    {
    	//todo: fix this

    //echo $locale;


    	/*if( self::$locale !== true ){
    		setlocale(LC_ALL ,self::$locale);
    		self::$locale = true;
    	}*/

    	setlocale(LC_ALL, $locale.'.UTF-8');



        $result = array(
            'day'		=>date('d',$time),
			'month'		=>date('M',$time),
			'dmonth'		=>date('m',$time),
			'year'		=>date('Y',$time),
			 'hour'		=>date('H',$time),
			 'minute'	=>date('i',$time),
			 'second'	=>date('s',$time),
			 'timezone'	=>date('e',$time),
			 'timestamp'=>$time,
			 'weekday'	=>date('N',$time),
			 'dayofyear'=>date('z',$time),
			 'week'		=>date('W',$time),
			 'gmtsecs'	=>date('Z',$time),
             'weekdayshort'	=> strftime('%a',$time),
			 'weekdayname'	=> strftime('%A',$time),
			 'monthname'	=> strftime('%B',$time),
            'locale'	=> $locale
		);
		/*	// Code  for Zend_Date
		  	$result = $date->toArray();
            $result['weekdayname'] = $date->toString("EEEE");
            $result['monthname'] = $date->toString("MMMM");
		*/
        return $result;
    }


    public static function getIpProxy(){
    	$user_ip = 0 ;
        if( $_SERVER['REMOTE_ADDR'])
        {$user_ip = $_SERVER['REMOTE_ADDR'];}
        elseif( $_SERVER['HTTP_FORWARDED_FOR'])
        {$user_ip = $_SERVER['HTTP_FORWARDED_FOR'];}
        elseif( $_SERVER['HTTP_X_FORWARDED_FOR'])
        {$user_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];}
        elseif( $_SERVER['HTTP_X_COMING_FROM'])
        {$user_ip = $_SERVER['HTTP_X_COMING_FROM'];}
        elseif( $_SERVER['HTTP_VIA'])
        {$user_ip = $_SERVER['HTTP_VIA'];}
        elseif( $_SERVER['HTTP_XROXY_CONNECTION'])
        {$user_ip = $_SERVER['HTTP_XROXY_CONNECTION'];}
        elseif( $_SERVER['HTTP_CLIENT_IP'])
        {$user_ip = $_SERVER['HTTP_CLIENT_IP'];}
        else{$user_ip='unknown';}


        return ip2long($user_ip);
    }

    public static function getIp(){
    	$user_ip = 0 ;
        if( $_SERVER['REMOTE_ADDR']){
            $user_ip = $_SERVER['REMOTE_ADDR'];
        }
        return ip2long($user_ip);
    }
}
